package com.addplus.townmall.server.queue.serviceimpl;

import com.addplus.townmall.server.api.constant.ErrorCode;
import com.addplus.townmall.server.api.exception.ErrorException;
import com.addplus.townmall.server.api.mapper.town.*;
import com.addplus.townmall.server.api.model.town.ShopProduct;
import com.addplus.townmall.server.api.model.town.ShopProductFight;
import com.addplus.townmall.server.api.model.town.expand.OptimalShopExpand;
import com.addplus.townmall.server.api.service.queue.ShopCartDetelteService;
import com.addplus.townmall.server.api.service.queue.ShopOnlineStatusService;
import com.addplus.townmall.server.api.utils.DataUtils;
import com.addplus.townmall.server.api.utils.date.DateUtils;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.time.LocalDateTime;
import java.util.List;

@Service(interfaceClass = ShopOnlineStatusService.class, async = true)
public class ShopOnlineStatusServiceImpl implements ShopOnlineStatusService {

    @Autowired
    ShopProductFightMapper shopProductFightMapper;

    @Autowired
    ShopProductMapper shopProductMapper;

    @Autowired
    SpikeActiveShopMapper spikeActiveShopMapper;

    @Autowired
    OptimalShopMapper otimalShopMapper;

    @Override
    public void ShopOnlineStatus(Integer id ) {

        if (DataUtils.EmptyOrNegativeOrZero(id)) {
            return;
        }
        System.out.println(id);
        //此处查询多表，修改多张表的商品状态为下架

        //0.猜你喜欢商品列表，该表无上下架状态

        //1.商城秒杀商品表，该表无上下架状态

        //2.商城优选商品表。
        List<OptimalShopExpand> list = otimalShopMapper.changeShopStatusById(id);
        if(!list.isEmpty()){
            for(OptimalShopExpand os:list){
                if(os.getOnlineStatus()!=null && os.getOnlineStatus()==1){
                    os.setOnlineStatus(0);
                    os.setGmtModified(DateUtils.nowDate());
                }
                int a = otimalShopMapper.updateById(os);
                if (a < 0) {
                    return;
                }
            }
        }

        //3.拼团商品列表
        ShopProduct shopProduct = shopProductMapper.selectById(id);
        if(shopProduct.getShopCode()!=null){
            String shopcode = shopProduct.getShopCode();
            ShopProductFight shopProductFight=shopProductFightMapper.getShopProductFightByShopCode(shopcode);

            if (DataUtils.isEmptyObject(shopProductFight)) {
                return;
            }
            if (shopProductFight.getOnlineStatus() == 0) {
                shopProductFight.setOnlineStatus(1);// 设置为上架状态
                shopProductFight.setOnlineTime(LocalDateTime.now());  //设置上架时间
            } else {
                shopProductFight.setOnlineStatus(0);
            }
            shopProductFight.setGmtModified(DateUtils.nowDate());
            int modifyProduct = shopProductFightMapper.updateById(shopProductFight);
            if (modifyProduct < 0) {
                return;
            }

        }

    }
}
